package com.ls.lserp.modules.setting.web;

import com.ls.lserp.common.persistence.AjaxDone;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.common.web.BaseController;
import com.ls.lserp.modules.setting.entity.CheckProjectEntity;
import com.ls.lserp.modules.setting.entity.OperatingRREntity;
import com.ls.lserp.modules.setting.service.CheckProjectService;
import com.ls.lserp.modules.sys.utils.OperatingUtils;

import freemarker.template.Configuration;
import groovy.util.ResourceException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: CheckProjectController
 * @Description: 订单导出项目数量控制
 * 
 * @author 于辉
 * @date 2017年5月27日
 *
 */

@Controller
@RequestMapping(value = "${adminPath}/check/project")
public class CheckProjectController extends BaseController{
	
	@Autowired
	// 保存 编辑 删除 方法
	private CheckProjectService checkProjectService;
    @Autowired
    private Configuration freemarkerConfiguration;
    @ModelAttribute
    public CheckProjectEntity get(@RequestParam(required = false) String id) {

        // 判断id是否为空
        if (StringUtils.isNotBlank(id)) {

            // 执行查询语句 查询基本信息
            return checkProjectService.get(Long.parseLong(id));
        } else {

            // 若为空，实例化
            return new CheckProjectEntity();
        }
    }
	
	/**
	 * 进入一览页面
	 * @param checkProjectEntity
	 * @param model
	 * @return
	 */
	@RequiresPermissions("check:project:view")
	@RequestMapping(value = {""})
	public String list(CheckProjectEntity checkProjectEntity, Model model) {
        checkProjectEntity.setOperatingId (checkProjectService.getOperatingMinId());
        CheckProjectEntity checkProjectEntityTemp = checkProjectService.selectValueNameCnList (checkProjectEntity);
        model.addAttribute ("itemObj",checkProjectEntityTemp);
		// 返回页面地址
	    return "modules/setting/checkProjectList";
	}

    /**
     * 更新
     * @param checkProjectEntity
     * @param response
     * @return
     */
    @RequiresPermissions("check:project:view")
    @RequestMapping(value = {"saveForm"})
    @ResponseBody
    public String saveForm(CheckProjectEntity checkProjectEntity, HttpServletResponse response, HttpServletRequest request) throws ResourceException {
           try{
               checkProjectService.insertDesId (checkProjectEntity);
	            // 清除缓存
	       		OperatingUtils.removeOperatingCache();
           } catch (Exception e) {
        	   logger.error(e.getMessage(),e);
                // 提示错误信息
               return renderString(response,AjaxDone.error(getText("System_error_please_contact_your_administrator")));
           }
            // ajax返回结果
             return renderString (response, AjaxDone.ok (ResourcesUtil.getFormatProperties ("messages_zh_CN", "audit_ok_msg", new String[]{""})));
    }


    /**
     * 更新
     * @param checkProjectEntity
     * @param response
     * @return
     */
    @RequiresPermissions("check:project:view")
    @RequestMapping(value = {"changeForm"})
    @ResponseBody
    public String changeForm(CheckProjectEntity checkProjectEntity, HttpServletResponse response, HttpServletRequest request) throws Exception {
        Map <Object, Object> listMap = new HashMap <Object, Object> ();
        Map<String, Object> map = new HashMap<String, Object>();
        CheckProjectEntity checkProjectEntityTemp = checkProjectService.selectValueNameCnList (checkProjectEntity);
        map.put("oneList", checkProjectEntityTemp.getTypeCdListOne ());
        map.put("twoList", checkProjectEntityTemp.getTypeCdListTwo ());
        map.put("fourList", checkProjectEntityTemp.getTypeCdListFour ());
        map.put("fiveList", checkProjectEntityTemp.getTypeCdListFive ());
        map.put("sixList", checkProjectEntityTemp.getTypeCdListSix ());
        map.put("sevenList", checkProjectEntityTemp.getTypeCdListSeven());
        map.put("operatingIdList", checkProjectEntityTemp.getOperatingREntityList ());
        map.put("operatingFitIdList", checkProjectEntityTemp.getOperatingFitREntityList ());
        map.put("springMacroRequestContext", new RequestContext (request));
        String strHtml = "";
        strHtml = FreeMarkerTemplateUtils
                .processTemplateIntoString(freemarkerConfiguration.getTemplate("checkProjectHtml.ftl"), map);
        listMap.put ("allCheckBoxHtml",strHtml);
        return renderString(response, listMap);
    }
    
    /**
     * 设置工序布局画面
     * @param response
     * @return
     */
    @RequiresPermissions("check:project:view")
    @RequestMapping(value = {"setSortImage"})
    public String setSortImage(Long operatingId,String operatingName,Model model){
    	Map<String,Object> map = checkProjectService.findItemByOperating(operatingId);
    	model.addAttribute("leftList",(List<OperatingRREntity>)map.get("partLeft"));
    	model.addAttribute("rightList",(List<OperatingRREntity>)map.get("partRight"));
    	model.addAttribute("operatingName",operatingName);
    	return "modules/setting/setOperatingSort";
    }
    
    /**
     * 更新
     * @param checkProjectEntity
     * @param response
     * @return
     */
    @RequiresPermissions("check:project:edit")
    @RequestMapping(value = {"saveSort"})
    @ResponseBody
    public String saveSort(OperatingRREntity operatingREntity, HttpServletResponse response, HttpServletRequest request) throws Exception {
        String flag = "false";
    	try {
    		checkProjectService.saveOperatingSortData(operatingREntity);
    		// 清除缓存
    		OperatingUtils.removeOperatingCache();
    		flag = "true";
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
    	return renderString(response, flag);
    }
}
